home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / system-tools-backends-2.0 / scripts / Utils / Monitor.pm < prev    next >
Encoding:
Perl POD Document  |  2009-04-09  |  2.3 KB  |  102 lines

  1. #-*- Mode: perl; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  
  3. # Authors: Carlos Garnacho Parro  <carlosg@gnome.org>
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU Library General Public License as published
  7. # by the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. # GNU Library General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Library General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  18.  
  19. package Utils::Monitor;
  20.  
  21. use Cwd;
  22. use strict;
  23. use base qw(Net::DBus::Object);
  24. use Utils::Backend;
  25. eval "use Sys::Gamin;";
  26. my $eval_gamin = $@;
  27.  
  28. my $has_gamin = ($eval_gamin eq "") ? 1 : 0;
  29. my $fm;
  30. my %objects;
  31.  
  32. if ($has_gamin)
  33. {
  34.   $fm = new Sys::Gamin;
  35. }
  36.  
  37. sub do_monitor_files
  38. {
  39.   my ($event, $data, $func, $path, $object);
  40.  
  41.   return if (!$has_gamin);
  42.  
  43.   while ($fm->pending)
  44.   {
  45.     $event = $fm->next_event;
  46.     
  47.     if ($event->type eq "change" ||
  48.         $event->type eq "create")
  49.     {
  50.       $data = $objects {$event->filename};
  51.       $object = $$data{"object"};
  52.  
  53.       $object->emit_signal ($$data{"signal"});
  54.     }
  55.   }
  56. }
  57.  
  58. sub add_file
  59. {
  60.   my ($file, $object, $name, $signal) = @_;
  61.   my ($path);
  62.   
  63.   $path = &Cwd::abs_path ($file);
  64.   return unless -f $path;
  65.  
  66.   $objects {$path} = { "object" => $object,
  67.                        "name"   => $name,
  68.                        "signal" => $signal};
  69.   $fm->monitor ($path);
  70. }
  71.  
  72. sub monitor_files
  73. {
  74.   my ($files, $object, $name, $signal) = @_;
  75.   my ($f);
  76.  
  77.   return if (!$has_gamin);
  78.  
  79.   if (ref $files eq "ARRAY")
  80.   {
  81.     foreach $f (@$files)
  82.     {
  83.       &add_file ($f, $object, $name, $signal);
  84.     }
  85.   }
  86.   else
  87.   {
  88.     &add_file ($files, $object, $name, $signal);
  89.   }
  90. }
  91.  
  92. sub init_file_monitor
  93. {
  94.   return if (!$has_gamin);
  95.  
  96.   # should not use internal stuff in $fm like that
  97.   Net::DBus::Reactor->main->add_read ($fm->{conn}->fd (),
  98.                                       Net::DBus::Callback->new(method => \&Utils::Monitor::do_monitor_files));
  99. }
  100.  
  101. 1;
  102.